package com.huizu.common.utils.wx;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.huizu.common.utils.DateUtils;
import com.huizu.common.utils.OssUtil;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;

public class WxUtils {

    //    用户端
    private final static String AppId = "wx45f86db9e12994b6";  //公众平台自己的appId
    private final static String AppSecret="7fcbf9bcd26cfc3622371c0d1129e8c5";  //AppSecret

    public final static String puAppId ="wx43b263d040dfd507"; //公众号Appid
    public final static String puAppSecret ="538208326f173631ae2265e2fba55d25";//密钥
    public final static String themId ="6d0wgXGquO-7xSPUp-Qa1uFy9cKd0gDN45jN-hynT8Q"; //公众号模板ID

    /**
     * 根据code获取用户信息 用户
     * @param code
     * @return
     */
    public static Code2sessionResponse getJscode2session(String code){
        //获取openid等值
        String url="https://api.weixin.qq.com/sns/jscode2session?" +
                "appid="+AppId +
                "&secret="+AppSecret +
                "&js_code="+code +
                "&grant_type=authorization_code";
        String jsonData = HttpClientUtil2.doGet(url);
        Code2sessionResponse code2sessionResponse = JSON.parseObject(jsonData, Code2sessionResponse.class);
        if (code2sessionResponse.getAccessToken()==null){
            code2sessionResponse.setAccessToken(getAccessToken());
        }
        return code2sessionResponse;
    }

    /**
     *         //获取access_token 加密token等值
     * @return
     */
    public static String getAccessToken(){
        String urlToken = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential" +
                "&appid="+AppId+
                "&secret=" +AppSecret;
        String s = HttpClientUtil2.doGet(urlToken);
        JSONObject jsonObject = JSONObject.parseObject(s);
        String access_token = (String)jsonObject.get("access_token");
        return access_token;
    }

    public static String getQuery(Map<String, Object> map) {
        String codeId = String.valueOf(map.get("codeId"));
        String scene = String.valueOf(map.get("scene"));
        String skipUrl = String.valueOf(map.get("skipUrl"));

        RestTemplate rest = new RestTemplate();
        try {
            String url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token="
                    + getAccessToken();
            Map<String, Object> param = new HashMap<>();
            param.put("scene", scene);
            param.put("page", skipUrl);
            param.put("width", 430);
            param.put("auto_color", false);
            param.put("env_version", "trial");//体验版
            param.put("check_path",false);
            Map<String, Object> line_color = new HashMap<>();
            line_color.put("r", 0);
            line_color.put("g", 0);
            line_color.put("b", 0);
            param.put("line_color", line_color);
            MultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
            HttpEntity requestEntity = new HttpEntity(param, headers);
            ResponseEntity<byte[]> entity = rest.exchange(url, HttpMethod.POST, requestEntity, byte[].class,
                    new Object[0]);
            System.out.println(entity);
            byte[] result = entity.getBody();
            String s = OssUtil.insertByte(result);
            return s;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取公众号token
     * @param map
     * @return
     */
    public static String exchangeAccessToken(Map<String, Object> map) {
        JSONObject json = new JSONObject();
//        SysToken accessToken = new SysToken();
        String access_Token = "";
        // 获取access_token的接口地址（GET） 限200（次/天）
        String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET"
                .replace("APPID",puAppId).replace("APPSECRET",puAppSecret);
        // 获得access_token
        String str = HttpUtil.get(url);
        // 保存access_token
        if (str != null) {
            json = JSONObject.parseObject(str);
            System.out.println(str);
            access_Token = json.get("access_token").toString();
            System.out.println(access_Token);
        }
        return access_Token;
    }




}
